package com.yuki.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.yuki.api.common.CommonPage;
import com.yuki.api.dto.OmsMoneyInfoParam;
import com.yuki.api.dto.OmsOrderDeliveryParam;
import com.yuki.api.dto.OmsOrderQueryParam;
import com.yuki.api.dto.OmsReceiverInfoParam;
import com.yuki.api.model.*;
import com.yuki.api.service.IOmsOrderService;
import com.yuki.provider.component.CancelOrderSender;
import com.yuki.provider.mapper.OmsOrderItemMapper;
import com.yuki.provider.mapper.OmsOrderMapper;
import com.yuki.provider.mapper.PmsSkuStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: Yuki Tadayoshi
 * @time: 2021/3/30 20:34
 */
@Service(
        version = "1.0.0",
        interfaceName = "com.yuki.api.service.IOmsOrderService",
        interfaceClass = IOmsOrderService.class
)
@Transactional
public class OmsOrderServiceImpl implements IOmsOrderService {
    @Autowired
    private OmsOrderMapper orderMapper;

    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;

    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;

    @Autowired
    private CancelOrderSender cancelOrderSender;

    @Autowired
    private CacheManager cacheManager;

    public void clearOrder() {
        cacheManager.getCache("OrderList").clear();
    }

    @Override
    @CacheEvict(cacheNames = {"Order"}, key = "#id", allEntries = true, beforeInvocation = true)
    public int delete(Long id) {
        clearOrder();
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andStatusEqualTo(4).andIdEqualTo(id);
        OmsOrder order = new OmsOrder();
        order.setDeleteStatus(1);
        return orderMapper.updateByExampleSelective(order, example);
    }

    @Cacheable(cacheNames = {"Order"}, unless = "#result==null", key = "#id")
    @Override
    public OmsOrder detail(Long id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public OmsOrder updateNote(Long id, String note, Integer status) {
        return null;
    }

    @Override
    public OmsOrder updateReceiverInfo(OmsReceiverInfoParam receiverInfoParam) {
        return null;
    }

    @Override
    public OmsOrder updateMoneyInfo(OmsMoneyInfoParam moneyInfoParam) {
        return null;
    }

    @Override
    public OmsOrder close(Long id, String note) {
        return null;
    }


    @Cacheable(cacheNames = {"OrderList"}, unless = "#result==null",
            key = "T(String).valueOf(#pageNum+'-'+#pageSize)" +
                    ".concat(#queryParam.orderSn!=null?#queryParam.orderSn:'os')" +
                    ".concat(#queryParam.receiverKeyword!=null?#queryParam.receiverKeyword:'r')" +
                    ".concat(#queryParam.status!=null?#queryParam.status:'s')" +
                    ".concat(#queryParam.orderType!=null?#queryParam.orderType:'ot')" +
                    ".concat(#queryParam.sourceType!=null?#queryParam.sourceType:'st')" +
                    ".concat(#queryParam.createTime!=null?#queryParam.createTime:'ct')")
    @Override
    public CommonPage list(OmsOrderQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<OmsOrder> list = orderMapper.getOrderList(queryParam);
        return CommonPage.restPage(list);
    }

    @Override
    public OmsOrder delivery(OmsOrderDeliveryParam deliveryParam) {
        return null;
    }

    @Override
    public void sendDelayMessageCancelOrder(Long orderId, int minute) {
        long delayTime = minute * 60 * 1000;
        cancelOrderSender.sendMessage(orderId,delayTime);
    }

    @CachePut(cacheNames = {"Order"}, key = "#orderId")
    @Override
    public OmsOrder cancelOrder(Long orderId) {
        clearOrder();
        OmsOrder cancelOrder = this.detail(orderId);
        if (cancelOrder != null) {
            cancelOrder.setStatus(4);
            orderMapper.updateByPrimaryKeySelective(cancelOrder);
            OmsOrderItemExample example = new OmsOrderItemExample();
            example.createCriteria().andOrderIdEqualTo(orderId);
            List<OmsOrderItem> orderItemsList = omsOrderItemMapper.selectByExample(example);
            List<Long> productIdList = orderItemsList.stream().map(c -> c.getProductId()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(productIdList)) {
                PmsSkuStock skuStock = new PmsSkuStock();
                PmsSkuStockExample example1 = new PmsSkuStockExample();
                example1.createCriteria().andProductIdIn(productIdList);
                skuStock.setLockStock(0);
                pmsSkuStockMapper.updateByExampleSelective(skuStock, example1);
            }
        }
        return cancelOrder;
    }
}
